package com.bp.scoringgun.service.impl;

import com.bp.scoringgun.entity.*;

import com.bp.scoringgun.service.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.sql.SQLException;

@Service
public class DatabaseHandlerServiceImpl implements DatabaseHandlerService {

    @Autowired
    private RepoService repoService ;
    @Autowired
    private UserService userService ;
    @Autowired
    private ContriService contriService ;
    @Autowired
    private OrgService orgService ;
    @Autowired
    private UserOrgService userOrgService ;

    // 向 Repo 表插入数据的方法
    public void writeRepo(Repo repo) throws SQLException {
        // 查询是否存在相同 ID 的 Repo
        LambdaQueryWrapper<Repo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Repo::getId, repo.getId());
        Repo existingRepo = this.repoService.getOne(wrapper);

        if (existingRepo != null) {
            // 如果已存在，进行字段累加和替换
            existingRepo.setNumFork(existingRepo.getNumFork() + repo.getNumFork());
            existingRepo.setNumWatch(existingRepo.getNumWatch() + repo.getNumWatch());
            existingRepo.setNumIssue(existingRepo.getNumIssue() + repo.getNumIssue());
            existingRepo.setNumPR(existingRepo.getNumPR() + repo.getNumPR());
            existingRepo.setNumSolvePR(existingRepo.getNumSolvePR() + repo.getNumSolvePR());
            existingRepo.setNumSolveIssue(existingRepo.getNumSolveIssue() + repo.getNumSolveIssue());
            existingRepo.setNumRelease(existingRepo.getNumRelease() + repo.getNumRelease());
            existingRepo.setNumMember(existingRepo.getNumMember() + repo.getNumMember());
            existingRepo.setMainlyUseLanguage(repo.getMainlyUseLanguage());  // 替换语言字段

            // 更新已有记录
            repoService.updateById(existingRepo);
            //System.out.println("Repo entry updated successfully: " + existingRepo);
        } else {
            // 如果不存在，插入新记录
            repoService.save(repo);
            //System.out.println("Repo entry inserted successfully: " + repo);
        }
    }

    // 向 User 表插入数据
    public void writeUser(User user) {
        // 查询是否存在相同 ID 的 User 记录
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getId, user.getId());
        User existingUser = this.userService.getOne(wrapper);

        if (existingUser != null) {
            // 更新 URL 字段
            existingUser.setUrl(user.getUrl());
            existingUser.setNation(user.getNation());
            existingUser.setField(user.getField());
            // 更新已有记录
            userService.updateById(existingUser);
            //System.out.println("User entry updated successfully: " + existingUser);
        } else {
            // 如果不存在，插入新记录
            try {
                userService.save(user);
                //System.out.println("User entry inserted successfully: " + user);
            }catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    // 向 Contri 表插入数据
    public void writeContri(Contri contri) throws SQLException {
        // 查询是否存在相同 userId 和 repoId 的记录
        LambdaQueryWrapper<Contri> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Contri::getUserId, contri.getUserId())
                .eq(Contri::getRepoId, contri.getRepoId());
        Contri existingContri = this.contriService.getOne(wrapper);

        if (existingContri != null) {
            // 如果已存在，进行字段累加和处理 isOwner 字段
            existingContri.setNumPush(existingContri.getNumPush() + contri.getNumPush());
            existingContri.setNumPR(existingContri.getNumPR() + contri.getNumPR());
            existingContri.setNumSolveIssue(existingContri.getNumSolveIssue() + contri.getNumSolveIssue());
            existingContri.setNumCreate(existingContri.getNumCreate() + contri.getNumCreate());

            // 如果新的记录是拥有者，则更新 isOwner 字段为 true
            if (contri.isOwner()) {
                existingContri.setOwner(true);
            }
            // 更新已有记录
            //System.out.println(contri.getUserId()+"  "+contri.getRepoId());
            contriService.updateById(existingContri);
            //System.out.println("Contri entry updated successfully: " + existingContri);
        } else {
            // 如果不存在，插入新记录
            contriService.save(contri);
            //System.out.println("Contri entry inserted successfully: " + contri);
        }
    }

    // 向 Org 表插入数据
    public void writeOrg(Org org) {
        // 查询是否存在相同 ID 的 Org 记录
        LambdaQueryWrapper<Org> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Org::getId, org.getId());
        Org existingOrg = this.orgService.getOne(wrapper);

        if (existingOrg != null) {
            // 更新 URL 字段
            existingOrg.setUrl(org.getUrl());
            existingOrg.setLocation(org.getLocation());
            // 更新已有记录
            orgService.updateById(existingOrg);
            //System.out.println("Org entry updated successfully: " + existingOrg);
        } else {
            // 如果不存在，插入新记录
            orgService.save(org);
            //System.out.println("Org entry inserted successfully: " + org);
        }
    }

    public void writeUserOrg(UserOrg userOrg) throws SQLException {
        // 查询是否存在相同 userId 和 orgId 的记录
        LambdaQueryWrapper<UserOrg> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserOrg::getUserId, userOrg.getUserId())
                .eq(UserOrg::getOrgId, userOrg.getOrgId());
        UserOrg existingUserOrg = this.userOrgService.getOne(wrapper);

        if (existingUserOrg != null) {
            // 如果已存在，可以选择更新操作或者直接跳过
            //System.out.println("UserOrg entry already exists: " + existingUserOrg);
        } else {
            // 如果不存在，插入新记录
            userOrgService.save(userOrg);
            //System.out.println("UserOrg entry inserted successfully: " + userOrg);
        }
    }

}